import config from './chartConfig';
import { cloneDeep, assign } from 'lodash';
import { moment } from 'istrong-timeutil'
import Highcharts from 'istrong-highcharts';

let mergeOptions = (list, beginTime, endTime) => {
  let chartOpt = cloneDeep(config);

  let waterData = [],
    inFlowData = [],
    outFlowData = [];

  let minWaterVal, maxWaterVal, maxFlowVal, minFlowVal;

  minWaterVal = list.length > 0 ? list[0].val : 0;
  maxWaterVal = list.length > 0 ? list[0].val : 100;
  minFlowVal = list.length > 0 ? list[0].in_val || list[0].out_val || Number.MAX_VALUE : 0;
  maxFlowVal = list.length > 0 ? list[0].in_val || list[0].out_val || Number.MIN_VALUE : 100;
  let minTime = list.length === 0 ? parseInt(moment(beginTime).format('x'), 10) : parseInt(moment(list[0].time).format('x'), 10);
  let maxTime = list.length === 0 ? parseInt(moment(moment(endTime).add('hour', 1).format('YYYY-MM-DDTHH:00:00')).format('x'), 10) :
    parseInt(moment(list[list.length - 1].time).format('x'), 10);

  list.forEach((item, i) => {
    let tm = parseInt(moment(item.time).format('x'), 10);
    waterData.push({
      x: tm,
      y: item.val
    });
    if (item.in_val) {
      inFlowData.push({
        x: tm,
        y: item.in_val
      });
    }
    if (item.out_val) {
      outFlowData.push({
        x: tm,
        y: item.out_val
      });
    }
    minWaterVal = Math.min(minWaterVal, item.val);
    maxWaterVal = Math.max(maxWaterVal, item.val);
    minFlowVal = Math.min(minFlowVal, Math.min(item.in_val || Number.MAX_VALUE, item.out_val || Number.MAX_VALUE));
    maxFlowVal = Math.max(maxFlowVal, Math.max(item.in_val || Number.MIN_VALUE, item.out_val || Number.MIN_VALUE));
  });
  minFlowVal = minFlowVal === Number.MAX_VALUE ? 0 : minFlowVal;
  maxFlowVal = maxFlowVal === Number.MIN_VALUE ? 100 : maxFlowVal;
  var yInterval_Water = parseFloat((((maxWaterVal + 1) - (minWaterVal - 1)) / 6).toFixed(2));
  var yInterval_Flow = parseFloat((((maxFlowVal + 1) - (minFlowVal - 1)) / 6).toFixed(2));

  let seriesMarker = {
    enabled: true,
    symbol: 'circle',
    radius: 4
  }
  chartOpt = assign(chartOpt, {
    chart: {
      type: 'line',
      marginRight: 50,
      marginLeft: 50,
      marginTop: 20,
      marginBottom: 40,
      alignTicks: false
    },
    xAxis: {
      labels: {
        formatter: function () {
          return moment(this.value).format('HH时<br/>DD日')
        }
      },
      lineColor: '#808080',
      lineWidth: 0,
      tickColor: '#808080',
      tickWidth: 1,
      tickLength: 5,
      tickPosition: 'outside',
      gridLineWidth: 1,
      endOnTick: true,
      startOnTick: true,
      tickAmount: 6,
      min: minTime,
      max: maxTime,
      tickInterval: 1000 * 3600
    },
    yAxis: [
      {
        lineWidth: 1,
        lineColor: '#808080',
        gridLineDashStyle: 'longdash',
        gridLineColor: '#E0E0E0',
        tickInterval: yInterval_Water,
        endOnTick: false,
        labels: {
          align: 'right',
          x: -3,
          y: 0,
          formatter: function () {
            return this.value.toFixed(2)
          }
        },
        title: {
          text: 'm',
          align: 'high',
          rotation: 0,
          x: 60,
          y: -12
        },
        max: maxWaterVal + 1,
        min: minWaterVal >= 0 ? Math.max(minWaterVal - 1, 0) : minWaterVal
      },
      {
        lineWidth: 1,
        opposite: true,
        lineColor: '#808080',
        tickInterval: yInterval_Flow,
        endOnTick: true,
        labels: {
          align: 'left',
          x: 5,
          y: 0,
          formatter: function () {
            return this.value.toFixed(2)
          }
        },
        title: {
          text: 'm³/s',
          align: 'high',
          rotation: 0,
          x: -70,
          y: -8
        },
        max: maxFlowVal + 1,
        min: minFlowVal >= 0 ? Math.max(minFlowVal - 1, 0) : minFlowVal
      }
    ],
    series: [{
      data: waterData,
      color: '#5399d7',
      marker: seriesMarker
    }, {
      data: inFlowData,
      color: '#FFC605',
      marker: seriesMarker,
      yAxis: 1
    }, {
      data: outFlowData,
      color: '#46aa37',
      marker: seriesMarker,
      yAxis: 1
    }]
  });
  return chartOpt;
}
let renderChart = (chartViewer, chartId, waterData, beginTime, endTime) => {
  if (chartViewer) {
    chartViewer.destroy();
  }
  let list = waterData.data,
    warn_val = waterData.warn_val,
    ensure_val = waterData.ensure_val;

  let chartOpt = mergeOptions(list, beginTime, endTime);
  chartOpt.chart.renderTo = chartId;
  chartViewer = new Highcharts.Chart(chartOpt);
  return chartViewer;
};

export default renderChart;
